package com.hengtiansoft.lfy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by suyiyang on 2016/10/27.
 */

public class CircleAnimView extends ImageView{

    private int radius = 0;
    private Paint mPaint;
    private int cx = 0;
    private int cy = 0;

    private int strokeWidth = 3;

    private int space = strokeWidth;
    //圆弧起始点
    private int startAngle = 270;
    //圆弧的角度
    private int sweepAngle = 10;

    private RectF rectF ;
    private boolean isStart = true;
    private Thread mThread;
    private int alpha = 210;

    private int time = 50;

    public CircleAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public CircleAnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public CircleAnimView(Context context) {
        super(context);
        initPaint();
    }

    /**
     * 设置画笔
     */
    private void initPaint() {
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true); //消除锯齿
        this.mPaint.setStyle(Paint.Style.STROKE);  //绘制空心圆或 空心矩形
        this.mPaint.setARGB(alpha,61, 119, 217);
        this.mPaint.setStrokeWidth(strokeWidth);
        rectF = new RectF();
    }

    /**
     * 计算半径
     */
    private void calRadius() {
        int width = getWidth();
        int height = getHeight();
        if(width > 0 &&  height > 0){
            radius =  width > height ? height/2 : width/2;
            cx = radius;
            cy = radius;
            radius -= space;
            rectF.top = cy - radius;
            rectF.left = cx - radius;
            rectF.right = cx + radius;
            rectF.bottom = cy + radius;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        calRadius();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,mPaint);
    }

    /**
     * 开始动画
     */
    public void startAnim(){
        if(mThread == null){
            mThread = new Thread(runnable);
            mThread.start();
        }
    }

    /**
     * 停止动画
     */
    public void stopAnim(){
        if(mThread != null){
            mThread.interrupt();
            mThread = null;
        }
    }
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        isStart = visibility == View.VISIBLE?true:false;
        sweepAngle = 0;
        if(visibility == View.VISIBLE){
            startAnim();
        }else{
            stopAnim();
        }
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(sweepAngle < 360){
                sweepAngle += 10;
            }else{
                sweepAngle = 10;
            }
            invalidate();
        }
    };

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while(isStart){
                try {
                    Thread.sleep(time);
                    mHandler.sendMessage(mHandler.obtainMessage());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}
